package com.agan.workflow.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.agan.common.core.constant.UserConstants;
import com.agan.common.core.utils.DateUtils;
import com.agan.common.core.utils.StringUtils;
import com.agan.common.core.utils.bean.BeanUtils;
import com.agan.workflow.domain.vo.TreeSelect;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.agan.workflow.mapper.BpmTypeMapper;
import com.agan.workflow.domain.BpmType;
import com.agan.workflow.service.IBpmTypeService;

/**
 * 流程分类Service业务层处理
 * 
 * @author agan
 * @date 2024-01-10
 */
@Service
@RequiredArgsConstructor
public class BpmTypeServiceImpl implements IBpmTypeService 
{
    private final BpmTypeMapper bpmTypeMapper;

    /**
     * 查询流程分类
     * 
     * @param typeId 流程分类主键
     * @return 流程分类
     */
    @Override
    public BpmType selectBpmTypeByTypeId(Long typeId)
    {
        return bpmTypeMapper.selectBpmTypeByTypeId(typeId);
    }

    /**
     * 查询流程分类列表
     * 
     * @param bpmType 流程分类
     * @return 流程分类
     */
    @Override
    public List<BpmType> selectBpmTypeList(BpmType bpmType)
    {
        return bpmTypeMapper.selectBpmTypeList(bpmType);
    }

    /**
     * 新增流程分类
     * 
     * @param bpmType 流程分类
     * @return 结果
     */
    @Override
    public int insertBpmType(BpmType bpmType)
    {
        bpmType.setCreateTime(DateUtils.getNowDate());
        return bpmTypeMapper.insertBpmType(bpmType);
    }

    /**
     * 修改流程分类
     * 
     * @param bpmType 流程分类
     * @return 结果
     */
    @Override
    public int updateBpmType(BpmType bpmType)
    {
        bpmType.setUpdateTime(DateUtils.getNowDate());
        return bpmTypeMapper.updateBpmType(bpmType);
    }

    /**
     * 批量删除流程分类
     * 
     * @param typeIds 需要删除的流程分类主键
     * @return 结果
     */
    @Override
    public int deleteBpmTypeByTypeIds(Long[] typeIds)
    {
        return bpmTypeMapper.deleteBpmTypeByTypeIds(typeIds);
    }

    /**
     * 删除流程分类信息
     * 
     * @param typeId 流程分类主键
     * @return 结果
     */
    @Override
    public int deleteBpmTypeByTypeId(Long typeId)
    {
        return bpmTypeMapper.deleteBpmTypeByTypeId(typeId);
    }

    /**
     * 获取流程分类定义树
     */
    @Override
    public List<TreeSelect> selectTypeTreeList(BpmType bpmType) {
        List<BpmType> types = selectBpmTypeList(bpmType);
        List<BpmType> treeList = buildTypeTreeSelect(types);
        if (BeanUtils.isEmpty(treeList)) {
            return new ArrayList<>();
        }
        return treeList.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 构建前端所需要下拉树结构
     */
    public List<BpmType> buildTypeTreeSelect(List<BpmType> types)
    {
        List<BpmType> resultTree = new ArrayList<>();
        for (BpmType bpmType : types) {
            if (UserConstants.TOP_ID.equals(bpmType.getParentId())) {
                resultTree.add(bpmType);
            }
        }
        if (StringUtils.isNotEmpty(resultTree)) {
            for (BpmType tree: resultTree) {
                tree.setChildren(getTypeChild(tree, types));
            }
        }
        return resultTree;
    }

    /**
     * 获取子节点列表
     */
    private List<BpmType> getTypeChild(BpmType tree, List<BpmType> types) {
        List<BpmType> result = new ArrayList<>();
        for (BpmType type : types) {
            if (tree.getTypeId().equals(type.getParentId())) {
                if (checkChild(type, types)) {
                    type.setChildren(getTypeChild(type, types));
                }
                result.add(type);
            }
        }
        return result;
    }

    /**
     * 校验是否包含子节点
     */
    private boolean checkChild(BpmType type, List<BpmType> types) {
        boolean flag = false;
        for (BpmType item : types) {
            if (item.getParentId().equals(type.getTypeId())) {
                flag = true;
                break;
            }
        }
        return flag;
    }

}
